/**
 * Created by yanwe on 2016/6/25.
 */

(function () {
    'use strict';
    app.controller("GoodsListController", ["$scope", "$http", "i18nService","$state", "goodsClass",function ($scope, $http, i18nService,$state,goodsClass) {
    	
    	$scope.pagingOptions = {
    			 pageSizes: [250, 500, 1000],
    			 pageSize: 15,
	             currentPage: 1,
	             totalPages:0,
	             status:1,
	             goodsName:'',
	             label:''
    	};
    	
    	
    	 $scope.gridOptions = {
                 paginationPageSizes: [15, 20, 25],
                 paginationPageSize: 15,
                 enableColumnResizing: true,
                 enableGridMenu: true,
                 columnDefs: [
                     {name: 'goodsNames', pinnedLeft: true, displayName: '商品名称', width: 200},
                     {name: 'deliveryTime', displayName: '发货时间', width: 190},
                     {name: 'status', displayName: '发货状态', width: 90},
                     {name: 'expressCode', displayName: '快递单号', width: 100},
                     {name: 'consignerName', displayName: '发货人姓名', width: 100},
                     {name: 'consignerTelphone', displayName: '发货人电话', width: 100},
                     {name: 'createTime', displayName: '订单时间', width: 190},
                     {
                         name: '详情', width: 110,
                         pinnedRight: true,
                         cellTemplate: '<button class="btn btn-mini btn-primary table-btn" ng-click="grid.appScope.showMe(this)">详情</button>'
                     }
                 ],
                 data: 'myData'
             };
    	
    	goodsClass.getGoodsList($scope.pagingOptions).then(function(data){
    		$scope.pagingOptions.currentPage = data.currentPage;
    		$scope.pagingOptions.goodsName = data.goodsName;
    		$scope.pagingOptions.pageSize = data.pageSize;
    		$scope.pagingOptions.totalPages = data.totalServerItems;
    		$scope.myData = data.data;
    	});
    	
    	
    	
    	
    	
    	
    	
        $scope.filterOptions = {
            filterText: "",
            useExternalFilter: true
        };
        i18nService.setCurrentLang('zh-cn');
        $scope.totalServerItems = 0;
        $scope.pagingOptions = {
            pageSizes: [250, 500, 1000],
            pageSize: 250,
            currentPage: 1
        };

        $scope.setPagingData = function (data, page, pageSize) {
            var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
            $scope.myData = pagedData;
            $scope.totalServerItems = data.length;
            if (!$scope.$$phase) {
                $scope.$apply();
            }
        };
        
        $scope.getPagedDataAsync = function (pageSize, page, condition) {
            setTimeout(function () {
                if (condition) {
                	goodsClass.getOrderList(condition.goodsName,condition.startTime,
                        condition.endTime,condition.label,page).success(function (largeLoad) {
                        $scope.setPagingData(largeLoad, page, pageSize);
                    });
                } else {
                	goodsClass.getOrderList('','','','',page).success(function (largeLoad) {
                        $scope.setPagingData(largeLoad, page, pageSize);
                    });
                }
            }, 100);
        };
        
        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

        $scope.$watch('pagingOptions', function (newVal, oldVal) {
            if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
            }
        }, true);
        $scope.$watch('filterOptions', function (newVal, oldVal) {
            if (newVal !== oldVal) {
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
            }
        }, true);
        $scope.showMe = function () {
            $state.go('app.goods.modify');
            alert($scope.someProp);
        };
        $scope.gridOptions = {
            paginationPageSizes: [25, 50, 75],
            paginationPageSize: 25,
            enableColumnResizing: true,
            enableGridMenu: true,
            columnDefs: [
                {name: 'name', pinnedLeft: true, displayName: '商品名称', width: 120},
                {name: 'topCategory', displayName: '一级分类', width: 100},
                {name: 'secondaryCategory', displayName: '二级分类', width: 100},
                {name: 'color', displayName: '颜色', width: 50},
                {name: 'tagPrice', displayName: '吊牌价', width: 200},
                {name: 'storagePrize', displayName: '入库价格', width: 200},
                {name: 'plusMaterial', displayName: '面料', width: 200},
                {
                    name:"state",displayName:'审核状态',width:100
                },
                {name: 'createTime', displayName: '申请时间', width: 200},
                {name: 'modifyTime', displayName: '修改时间', width: 200},
                {name: 'plusMaterial', displayName: '面料', width: 200},

                {
                    name: '详情', width: 100,
                    pinnedRight: true,
                    cellTemplate: '<button class="btn btn-mini btn-primary table-btn" ng-click="grid.appScope.showMe()">查看</button>'
                }

            ],
            data: 'myData'
        };
        //////////////////////////////
        $scope.engineer = {
            name: "Dani",
            currentActivity: {
                id: 3,
                type: "Work",
                name: "Fixing bugs"
            }
        };

        $scope.activities = [
                {id: 1, type: "一级分类", name: "Writing code"},
                {id: 2, type: "一级分类", name: "Testing code"},
                {id: 3, type: "一级分类", name: "Fixing bugs"},
                {id: 4, type: "二级分类", name: "Dancing"}];
    }]);
})();
